<?php

class AdminapiController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {

    }

    public function loginAction()
    {
        $data = $this->request->getPost('data');
        $my_data = array();
        parse_str($data,$my_data);
        $result = array();
        $result['status']= true;

        $username = $my_data['username'];
        $password = $my_data['password'];

        $is_existing = AdminAccount::findFirst(array(
                "conditions"=>"(username = ?0 or email = ?0) and password = ?1",
                "bind"=>array(0=>$username,1=>md5($password))
            ));
        if($is_existing)
        {
            if($is_existing->status == 0)
            {
                $result['status'] = false;
                $result['messages'] = "Your account is deactivated";
                echo json_encode($result);
                $this->view->disable();
                exit();
            }
            $this->session->set('admin_id',$is_existing->id);
            define("ADMIN_ID",$this->session->get('admin_id'));
            $is_existing->last_login= CURR_DATE;
            @$is_existing->save();    
        }
        $result['status'] = $is_existing;
        
        
        echo json_encode($result);
        $this->view->disable();

    }
    public function editAccountAction()
    {
        $data = $this->request->getPost('data');
        $admin_id_2 = $this->request->getPost('admin_id');
        $my_data = array();
        parse_str($data,$my_data); 
        $result = array();
        $result['status'] = true;   

        $auth_pass = md5($my_data['authorization_password']);
        $check_authorization = AdminAccount::findFirst(array(
                "conditions"=>"id = ?0 and password = ?1",
                "bind"=>array(0=>$this->session->get('admin_id'),1=>$auth_pass)
            ));
        if(!$check_authorization)
        {
           $result['status'] = false;
           $result['messages'] = "Authorization password is incorrect";
           echo json_encode($result);
           $this->view->disable();
           exit();
        }

        $account = AdminAccount::findFirstById($admin_id_2);
        $err_message = "";
        if(!$account->save($my_data)){
            $result['status'] = false;
            foreach ($account->getMessages() as $value) {
                # code...
                $err_message.=$value."\n";
            }
            $result['messages'] = $err_message;
        }

        echo json_encode($result);
        $this->view->disable();

    }
    public function createAccountAction()
    {
    	$data = $this->request->getPost('data');
    	$my_data = array();
    	parse_str($data,$my_data); 
    	$result = array();
		$result['status'] = true;   

        $auth_pass = md5($my_data['authorization_password']);
        $check_authorization = AdminAccount::findFirst(array(
                "conditions"=>"id = ?0 and password = ?1",
                "bind"=>array(0=>$this->session->get('admin_id'),1=>$auth_pass)
            ));
        if(!$check_authorization)
        {
           $result['status'] = false;
           $result['messages'] = "Authorization password is incorrect";
           echo json_encode($result);
           $this->view->disable();
           exit();
        }

    	$account = new AdminAccount();
    	$err_message = "";
    	if(!$account->save($my_data)){
    		$result['status'] = false;
    		foreach ($account->getMessages() as $value) {
    			# code...
    			$err_message.=$value."\n";
    		}
    		$result['messages'] = $err_message;
    	}

    	echo json_encode($result);
    	$this->view->disable();

    }
}

